﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;

namespace Kiosk.UserControls
{
    /// <summary>
    /// Interaction logic for MapPresenter.xaml
    /// </summary>
    public partial class MapPresenter : UserControl
    {
        public Point StartPoint 
        {
            get
            {
                return ellStartPoint.Center;
            }
            set
            {
              startRipple.Center = ellStartPoint.Center = value;
            }
        }
        public Point EndPoint 
        {
            get 
            {
                return ellEndPoint.Center;
            }
            set
            {
                endRipple.Center = ellEndPoint.Center = value;
            }
        }
        public MapPresenter()
        {
            InitializeComponent();
            this.StartPoint = new Point();
            this.EndLocationName = "";

        }

        private string _endLoc = "";
        public String EndLocationName
        {
            get
            {
                return _endLoc;
            }
            set
            {
                _endLoc = value;
                RefreshLabels();
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
                Storyboard sb = (Storyboard)this.Resources["sbPathAnimation"];
                if (sb != null)
                {
                    sb.Begin(this);
                }
                Storyboard ripple = (Storyboard)this.Resources["RipplePoints"];
                if (ripple != null)
                {
                    ripple.Begin(this);
                }


                RefreshLabels();
                        
                //Library.NotificationInfo ni = ((Library.NotificationInfo)this.DataContext);
                //if (ni != null)
                //{
                //    ellEndPoint.Center = ni.LocationPoint;
                //}
                //ellStartPoint.Center = this.StartPoint;
        }

        public void RefreshLabels()
        {
            SetStartText();
            SetEndText();
        }

        private void SetEndText()
        {
            Point p = new Point(this.EndPoint.X -25 , this.EndPoint.Y + 18);
            FormattedText ft = new FormattedText(EndLocationName,
                System.Globalization.CultureInfo.CurrentCulture,
                    FlowDirection.LeftToRight,
                    new Typeface(new FontFamily("Calibri"), FontStyles.Normal,
                                FontWeights.Normal,
                                FontStretches.Normal),
                    18, Brushes.Black);
            Geometry textGeo = ft.BuildGeometry(p);



            // create the bounding box
            Rect rect = textGeo.Bounds;
            double growBy = 8;
            if (!rect.IsEmpty)
            {
                rect.Height += growBy;
                rect.Width += growBy;
                rect.Y -= growBy / 2;
                rect.X -= growBy / 2;
            }
            
            RectangleGeometry rg = new RectangleGeometry(rect, 5,5);

            pthEndText.Data = textGeo;
            pthEndBorder.Data = rg;
        }

        private void SetStartText()
        {
            Point p = new Point(this.StartPoint.X - 25, this.StartPoint.Y + 18);
                FormattedText ft = new FormattedText("You are here",
                    System.Globalization.CultureInfo.CurrentCulture,
                        FlowDirection.LeftToRight,
                        new Typeface(new FontFamily("Calibri"), FontStyles.Normal,
                                    FontWeights.Normal, 
                                    FontStretches.Normal),
                        18, Brushes.Black);
                Geometry textGeo = ft.BuildGeometry(p);
                pthStartText.Data = textGeo;

            // create the bounding box
            Rect rect = textGeo.Bounds;
            double growBy = 8;
            if (!rect.IsEmpty)
            {
                rect.Height += growBy;
                rect.Width += growBy;
                rect.Y -= growBy / 2;
                rect.X -= growBy /2;
            }
            
            RectangleGeometry rg = new RectangleGeometry(rect, 5,5);

            pthStartBorder.Data = rg;

        }
    }
}
